﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using EShop.Model;
using EShop.DAL;
using EShop.Common;

namespace EShop.DAL
{
    public class MerchandiseClassDAL:BaseDAL
    {
               
        
        /// <summary>
        /// 取所有商品分类数据
        /// </summary>
        /// <returns></returns>
        public List<MerchandiseClass> GetAllMerchandiseClassList()
        {
            string strUSP = "USP_Merchandise_Class_GetList";
            List<MerchandiseClass> aMerchandiseClassList = new List<MerchandiseClass>();

            try
            {
                SqlDataReader reader = (SqlDataReader)DbHelper.ExecuteReaderSQL(strUSP, CommandType.StoredProcedure);
                while (reader.Read())
                {
                    MerchandiseClass oinf = new MerchandiseClass();
                    oinf.MerchandiseClassID = reader.GetInt32(0);
                    oinf.MerchandiseClassName = reader[1].ToString();
                    aMerchandiseClassList.Add(oinf);
                }
                reader.Close();

                return aMerchandiseClassList;
            }
            catch (Exception ex)
            {
                log.Write((string.Format("USP: {0}, \r\n ex : {1}", strUSP, ex.Message)));
                throw;
            }
        }


        /// <summary>
        /// 添加商品类型
        /// 如果添加失败，返回的Table为Null
        /// </summary>
        /// <param name="oMerchandiseClass"></param>
        /// <returns></returns>
        public System.Data.DataTable AppendMerchandiseClass(EShop.Model.MerchandiseClass oMerchandiseClass)
        {
            string strUSP = "USP_AppendMerchandiseClass";
            try
            {
                SqlParameter[] parameters ={
                                              new SqlParameter("@MerchandiseClassName",System.Data.SqlDbType.VarChar,50),
                                              new SqlParameter("@MerchandiseClassResume",System.Data.SqlDbType.Text),
                                              new SqlParameter("@MerchandiseClassRemark",System.Data.SqlDbType.Text)
                                           };
                parameters[0].Value = oMerchandiseClass.MerchandiseClassName;
                parameters[1].Value = oMerchandiseClass.MerchandiseClassResume;
                parameters[2].Value = oMerchandiseClass.MerchandiseClassRemark;
                return DbHelper.ExecuteDataSetSQL(strUSP, System.Data.CommandType.StoredProcedure, parameters).Tables[0];
            }
            catch (Exception ex)
            {
                log.Write((string.Format("USP: {0}, \r\n ex : {1}", strUSP, ex.Message)));
                throw;
            }
        }

        /// <summary>
        /// 查询商品类型
        /// 如果商品类型ID为空，则返回所有的商品类型ID和商品类型名称
        /// 否则，则返回指定商品类型ID的相关信息
        /// </summary>
        /// <param name="strMerchandiseClassID"></param>
        /// <returns></returns>
        public System.Data.DataTable QueryMerchandiseClass(string strMerchandiseClassID)
        {
            string strUSP = "USP_QueryMerchandiseClass";
            try
            {
                if (string.IsNullOrEmpty(strMerchandiseClassID))
                {
                    return DbHelper.ExecuteDataSetSQL(strUSP, System.Data.CommandType.StoredProcedure).Tables[0];
                }
                else
                {
                    SqlParameter[] parameters = { new SqlParameter("@MerchandiseClassID", System.Data.SqlDbType.Int) };
                    parameters[0].Value = strMerchandiseClassID;
                    return DbHelper.ExecuteDataSetSQL(strUSP, System.Data.CommandType.StoredProcedure, parameters).Tables[0];
                }
            }
            catch (Exception ex)
            {
                log.Write((string.Format("USP: {0}, \r\n ex : {1}", strUSP, ex.Message)));
                throw;
            }
        }

        /// <summary>
        /// 修改商品类型数据
        /// 
        /// </summary>
        /// <param name="oMerchandiseClass"></param>
        /// <returns></returns>
        public bool UpdateMerchandiseClass(EShop.Model.MerchandiseClass oMerchandiseClass)
        {
            string strUSP = "USP_UpdateMerchandiseClass";
            try
            {
                SqlParameter[] parameters = {
                                          new SqlParameter("@MerchandiseClassID",System.Data.SqlDbType.Int),
                                          new SqlParameter("@MerchandiseClassName",System.Data.SqlDbType.VarChar,50),
                                          new SqlParameter("@MerchandiseClassResume",System.Data.SqlDbType.Text),
                                          new SqlParameter("@MerchandiseClassRemark",System.Data.SqlDbType.Text)
                                      };
                parameters[0].Value = oMerchandiseClass.MerchandiseClassID;
                parameters[1].Value = oMerchandiseClass.MerchandiseClassName;
                parameters[2].Value = oMerchandiseClass.MerchandiseClassResume;
                parameters[3].Value = oMerchandiseClass.MerchandiseClassRemark;
                int count = DbHelper.ExecuteNonQuerySQL(strUSP, System.Data.CommandType.StoredProcedure, parameters);
                if (count > 0) return true;
                else return false;
            }
            catch (Exception ex)
            {
                log.Write((string.Format("USP: {0}, \r\n ex : {1}", strUSP, ex.Message)));
                throw;
            }
        }
    }
}
